#import "TransitionViewController.h"
#import <QuartzCore/QuartzCore.h>

@implementation TransitionViewController

@synthesize rootView;

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImage *image1 = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image1.jpg" ofType:nil]];
	view1 = [[UIImageView alloc] initWithImage:image1];
    
	UIImage *image2 = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image2.jpg" ofType:nil]];
	view2 = [[UIImageView alloc] initWithImage:image2];
	view2.hidden = YES;
    
	[rootView addSubview:view1];
	[rootView addSubview:view2];
    
	[view1 release];
	[view2 release];
    
	transitioning = NO;
}

- (void)dealloc {
    [rootView release];
    [super dealloc];
}

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag {
	transitioning = NO;
}

- (IBAction)performTransition:(id)sender {
    if (!transitioning) {
        CATransition *transition = [CATransition animation];
        transition.duration = 1.0;
        transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        
        transition.type = @"cube";
		transition.subtype = kCATransitionFromRight;
        
        transitioning = YES;
        transition.delegate = self;
        
        [rootView.layer addAnimation:transition forKey:nil];
        
        view1.hidden = YES;
        view2.hidden = NO;
        
        UIImageView *tmp = view2;
        view2 = view1;
        view1 = tmp;
	}
}

@end
